package com.example.sty.service.impl;


import com.example.sty.domain.UserProject;
import com.example.sty.domain.UserProjectVO;
import com.example.sty.mapper.ProjectMapper;
import com.example.sty.mapper.UserMapper;
import com.example.sty.mapper.UserProjectMapper;
import com.example.sty.service.ProjectService;
import com.example.sty.domain.Project;
import com.example.sty.domain.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserProjectMapper userProjectMapper;
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 发布运动项目
     *
     * @param project
     */
    @Override
    public void insertProject(Project project) {
        projectMapper.insertProject(project);
    }

    /**
     * 奖项颁布
     *
     * @param userProject
     */
    @Override
    public void insertUserProject(UserProject userProject) {
        userProjectMapper.insertUserProject(userProject);
    }

    /**
     * 查询所有的运动项目
     *
     * @return
     */
    @Override
    public List<Project> getProjects() {
        return projectMapper.getProjects();
    }

    /**
     * 查看单个项目的报名人信息
     *
     * @return
     */
    @Override
    public List<UserProjectVO> getUserProjects(Integer projectId) {
        List<UserProject> userProjects = userProjectMapper.getUserProjects(projectId);
        Project projectInfo = projectMapper.getProjectInfo(projectId);
        List<UserProjectVO> userProjectVOS = new ArrayList<>();
        for (UserProject userProject : userProjects) {
            UserProjectVO userProjectVO = new UserProjectVO();
            BeanUtils.copyProperties(userProject,userProjectVO);
            userProjectVO.setUser(userMapper.getUserInfo(userProject.getUserId()));
            userProjectVO.setProject(projectInfo);
            userProjectVO.setName(projectInfo.getName());
            userProjectVO.setRules(projectInfo.getRules());
            userProjectVO.setAddress(projectInfo.getAddress());
            userProjectVO.setStarTime(projectInfo.getStarTime());
            userProjectVOS.add(userProjectVO);
            userProjectVO.setUsername(userProjectVO.getUser().getUsername());
        }
        return userProjectVOS;
    }
    /**
     * 裁判员打分
     *
     * @param userProject
     */
    @Override
    public void insertScore(UserProject userProject) {
        userProjectMapper.insertUserProject(userProject);
    }

    /**
     * 查询受伤人员
     *
     * @return
     */
    @Override
    public List<User> getIsInjured() {
        return userMapper.getIsInjuredUsers();
    }
    @Override
    public List<UserProjectVO> getAllUserProjects() {
        List<Project> projects = projectMapper.getProjects();
        List<UserProjectVO> userProjectVOS = new ArrayList<>();
        for (Project project : projects) {
            List<UserProjectVO> userProjects = getUserProjects(project.getId());
            userProjectVOS.addAll(userProjects);
        }
        return userProjectVOS;
    }
}
